package hiatao.puzzle.adapter;

import haitao.puzzle.R;
import hiatao.puzzle.entity.ChatMessage;
import hiatao.puzzle.utils.ImageUtils;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MessageAdapter extends BaseAdapter {

	private List<ChatMessage> ChatMessageList =new ArrayList<ChatMessage>();
	private Context context;
	private LayoutInflater inflater;
	public MessageAdapter(Context context,  List<ChatMessage> newsList) {
		if(newsList!=null){
			this.ChatMessageList = newsList;
		}
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return ChatMessageList==null ? 0:ChatMessageList.size() ;
	}

	@Override
	public Object getItem(int position) {
		return ChatMessageList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	@Override
	public int getViewTypeCount() {
		return 2;
	}
	public void appendMessage(ChatMessage chatMessage) {
		ChatMessageList.add(chatMessage);
		notifyDataSetChanged();
	}
	public void appendToList(List<ChatMessage> addList) {
		
		if (addList == null) {
			return;
		}
		if (ChatMessageList == null) {
			ChatMessageList = new ArrayList<ChatMessage>();
		}
		ChatMessageList.addAll(addList);
		notifyDataSetChanged();
	}
	
	@Override
	public int getItemViewType(int position) {
		ChatMessage message = ChatMessageList.get(position);
		if (message.isSend()) {
			return -1;
		} else {
			return 0;
		}
	}
	public void clearAll() {
	    ChatMessageList.clear();
	    notifyDataSetChanged();
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final MessageHodler holder ;
		final ChatMessage message = ChatMessageList.get(position);
		if(convertView == null){
			convertView = createViewByMessage(message.isSend());
			if(!message.isSend()){
				holder = new MessageHodler();
				holder.image = (ImageView) convertView.findViewById(R.id.head_image);
				holder.tv_chatcontent = (TextView) convertView.findViewById(R.id.tv_chatcontent);
				convertView.setTag(holder);
			}else{
				holder = new MessageHodler();
				holder.image = (ImageView) convertView.findViewById(R.id.head_image);
				holder.tv_chatcontent = (TextView) convertView.findViewById(R.id.tv_chatcontent);
				convertView.setTag(holder);
			}

		}
		else{
			holder = (MessageHodler) convertView.getTag();
		}
		holder.tv_chatcontent.setText(message.getContent());
		ImageUtils.loadHeadImg(message.getUserHead(), holder.image);
		return convertView;
	}

	private View createViewByMessage(boolean isSend) {
		if (isSend){
			return  inflater.inflate(R.layout.row_sent_message, null);
		}else{
			return  inflater.inflate(R.layout.row_receive_message, null);
		}
	}
	private final class MessageHodler {
		public ImageView image;
		public TextView tv_chatcontent;

	}
}
